﻿using System.Security.Permissions;
using System.Security;
using System.IO;

namespace BloggerAutoPosterService.Engine.Utils
{
    public static class CheckPermission
    {
        public static bool HasWritePermission(string filename)
        {
            if (string.IsNullOrEmpty(filename)) return false;
            var info = new FileInfo(filename);
            var writePermission = new FileIOPermission(FileIOPermissionAccess.Write, info.FullName);
            if (SecurityManager.IsGranted(writePermission))
            {
                return true;
            }
            else
            {
                var filePermission = new FileIOPermission(FileIOPermissionAccess.AllAccess, filename);
                try
                {
                    filePermission.Demand();
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }
    }
}
